package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"practice-hub/handler"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.POST("/login", handler.Login)
	r.POST("/addUserRole", handler.AddUserRole)
	r.POST("/addRoleNode", handler.AddRoleNode)
	r.GET("/userRoleList", handler.UserRoleList)
	r.GET("/roleNodeList", handler.RoleNodeList)
	r.GET("/userList", handler.UserList)

	repo := r.Group("/repo")
	repo.POST("/addRepo", handler.AddRepo)
	repo.POST("/addRoleOrder", handler.AddRoleOrder)
	repo.GET("/roleOrderList", handler.RoleOrderList)

	role := r.Group("/repo")
	role.GET("/userLists", handler.UserLists)

	good := r.Group("/good")
	good.POST("/addGood", handler.AddGood)
	good.POST("/addGoodOrder", handler.AddGoodOrder)
	good.GET("/goodOrderList", handler.GoodOrderList)

	person := r.Group("/person")
	person.POST("/addPerSon", handler.AddPerSon)
	person.POST("/addPerSonOrder", handler.AddPerSonOrder)
	person.GET("/perSonOrderList", handler.PerSonOrderList)

	car := r.Group("/car")
	car.POST("/login", handler.CarLogin)
	car.POST("/addRole", handler.AddCarRole)
	car.POST("/addNode", handler.AddCarNode)
	car.GET("/userRoleList", handler.UserRoleLists)
	car.GET("/roleNodeList", handler.RoleNodeLists)
	car.POST("/addCar", handler.AddCar)
	car.POST("/addCarOrder", handler.AddCarOrder)
	car.GET("/carOrderList", handler.CarOrderList)

	bigcar := r.Group("/bigcar")
	bigcar.POST("/login", handler.Logins)
	bigcar.POST("/addRole", handler.AddBigCarRole)
	bigcar.POST("/addNode", handler.AddBigCarNode)
	bigcar.GET("/bigCarUserRole", handler.BigCarUserRole)
	bigcar.GET("/bigCarRoleNode", handler.BigCarRoleNode)
	bigcar.POST("/addBigCar", handler.AddBigCar)
	bigcar.POST("/addBigCarOrder", handler.AddBigCarOrder)
	bigcar.GET("/bigCarOrderList", handler.BigCarOrderList)

	sysgood := r.Group("/sysgood")
	sysgood.POST("/login", handler.Loginss)
	sysgood.POST("/addSysRole", handler.AddSySRole)
	sysgood.POST("/addSysNode", handler.AddSysNode)
	sysgood.POST("/sysUserRole", handler.SysUserRole)
	sysgood.POST("/sysRoleNode", handler.SysRoleNode)
	sysgood.POST("/addSysGood", handler.AddSysGood)
	sysgood.POST("/addSysGoodOrder", handler.AddSysGoodOrder)
	sysgood.GET("/sysGoodOrderList", handler.SysGoodOrderList)

	login := r.Group("/login")
	login.POST("/login", handler.Loginsss)
	login.POST("/addLoginRole", handler.AddLoginRole)
	login.POST("/addLoginNode", handler.AddLoginNode)
	login.POST("/loginUserRole", handler.LoginUserRole)
	login.POST("/loginRoleNode", handler.LoginRoleNode)
	login.POST("/addLoginGood", handler.AddLoginGood)
	login.POST("/addLoginGoodOrder", handler.AddLoginGoodOrder)
	login.GET("/loginOrderList", handler.LoginOrderList)
	return r
}
